package com.lmf.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmf.eduservice.entity.EduSubject;
import com.lmf.eduservice.entity.excel.SubjectData;
import com.lmf.eduservice.entity.subject.OneSubject;
import com.lmf.eduservice.entity.subject.TwoSubject;
import com.lmf.eduservice.listener.SubjectExcelListener;
import com.lmf.eduservice.mapper.EduSubjectMapper;
import com.lmf.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmf.handler.Exception.LmfException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author lmf
 * @since 2022-01-22
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //保存添加的课程，通过excel文件添加课程科目
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) throws LmfException {
        try{
            //获取上传文件的输入流
            InputStream inputStream = file.getInputStream();
            //调用easyExcel进行读取xlsx文件数据：一行一行的读取数据
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new LmfException(20002,"添加课程分类失败");
        }
    }

    //获取所有的一级分类课程，每个一级分类课程里面都包含二级分类课程
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询出所有的一级分类科目: parent_id = 0
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id","0");
        List<EduSubject> oneEduSubjectList = this.list(queryWrapper);

        //2.查询出所有的二级分类科目: parent_id != 0
        QueryWrapper<EduSubject> queryWrapper2 = new QueryWrapper<>();
        queryWrapper.ne("parent_id","0");
        List<EduSubject> twoEduSubjectList = this.list(queryWrapper2);

        //创建一个集合，作为最后返回的集合
        List<OneSubject> oneSubjectFinallyList = new ArrayList<>();
        //3.封装一级分类科目
        for (EduSubject oneEduSubject : oneEduSubjectList) {
            //利用spring的工具类将查询到的对象属性值，赋给一级分类对应实体类属性；
            // 当然你也可以手动一个个赋值给一级分类对应实体类
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(oneEduSubject,oneSubject);

            //4.封装二级分类科目
            for (EduSubject twoEduSubject : twoEduSubjectList) {
                //一级分类课程的id值，就是二级分类科目的parent_id值
                if (oneEduSubject.getId().equals(twoEduSubject.getParentId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(twoEduSubject,twoSubject);
                    //将二级分类科目添加进相应的一级分类科目
                    oneSubject.getChildren().add(twoSubject);
                }
            }

            //将封装好的一级分类科目，添加进最后返回的集合
            oneSubjectFinallyList.add(oneSubject);
        }

        //5.将最终结果返回：一个封装一级分类科目的list集合
        return oneSubjectFinallyList;
    }


}
